package landian.com.module_base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;

import com.jaeger.library.StatusBarUtil;
import com.orhanobut.logger.Logger;

import org.simple.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import landian.com.module_base.mvp.IView;
import landian.com.module_base.utils.ToastUtil;

/**
 * Created by Administrator on 2019-03-02.
 */

public abstract class BaseActivity<P extends BasePresenter> extends FragmentActivity implements IView {
    public Intent mIntent;
    private Unbinder binder;
    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        mIntent = getIntent();
        if (enableButterKnife()) {
            binder = ButterKnife.bind(this);
        }
        Logger.i(this.getClass().getSimpleName());
        if (enableStatusUtil()) {
            StatusBarUtil.setColor(this, ContextCompat.getColor(getContext(), R.color.color_blue), 0);
        }
        if (enableEventBus()) {
            EventBus.getDefault().register(this);
        }
        mPresenter = initPresenter();
        if (mPresenter != null)
            mPresenter.attachView(this);
        initView(mIntent);
    }

//    @Override
//    public void setContentView(@LayoutRes int layoutResID) {
//        super.setContentView(layoutResID);
//        if (enableButterKnife()) {
//            binder = ButterKnife.bind(this);
//        }
//    }

    protected abstract int getContentView();

    protected abstract void initView(Intent intent);

    protected abstract P initPresenter();

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void hideLoadingDialog() {

    }

    @Override
    public boolean enableButterKnife() {
        return true;
    }

    @Override
    public boolean enableStatusUtil() {
        return true;
    }

    @Override
    public boolean enableEventBus() {
        return false;
    }

    @Override
    public void toastTip(String msg) {
        ToastUtil.showLongToast(getContext(), msg);
    }

    @Override
    public void toastTip(int msgId) {
        ToastUtil.showLongToast(getContext(), msgId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (enableButterKnife()) {
            binder.unbind();
        }
        if (enableEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }
}
